package com.hundsun.cprs.yyt.service.form.field;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.hundsun.cprs.yyt.service.form.dto.FormAttrInfo;

/**
 * jquery.validate.js,jquery.metadate.js 校验规则解析工具
 * @author linrl
 * @date 2019年1月10日
 */
public class JqValidateUtil {
    
    public static String parseValidRule(FormAttrInfo formAttr){
        Map<String,Object> validRule = new HashMap<String,Object>();
        if ("Y".equalsIgnoreCase(formAttr.getIsRequired())) {
            validRule.put("required", true);
        }
        if (StringUtils.isNotEmpty(formAttr.getCheckRule())) {
            validRule.put("checkRule", formAttr.getCheckRule());
        }
        
        {
            Map<String,String> customeMessages = new HashMap<String,String>();
            if (StringUtils.isNotEmpty(formAttr.getIsRequiredTips())) {
                customeMessages.put("required", formAttr.getIsRequiredTips());
            }
            if (StringUtils.isNotEmpty(formAttr.getCheckRuleTips())) {
                customeMessages.put("checkRule", formAttr.getCheckRuleTips());
            }
            
            if (!customeMessages.isEmpty()) {
                validRule.put("messages", customeMessages);
            }
        }
        return validRule.isEmpty()?null:JSON.toJSONString(validRule).replaceAll("\"(\\w+)\"(\\s*:\\s*)", "$1$2").replaceAll("\"", "'");
    }
}
